package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.LoadingActivity;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.upload.bean.MonthPlanBean;
import com.cmcc.linlaker.ui.upload.bean.PlanBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.DensityUtil;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SPHelper;
import com.cmcc.linlaker.volley.GsonRequestHelper;
import com.dsw.calendar.component.MonthView;
import com.dsw.calendar.entity.CalendarInfo;
import com.dsw.calendar.theme.IDayTheme;
import com.dsw.calendar.theme.IWeekTheme;
import com.dsw.calendar.views.GridCalendarView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckPlanActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_plan);

        BarUtils.initStatusBar(this);

        initView();
    }

    /**
     * 数字小于10 前面加0
     */
    private String parseNumber(int number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return number + "";
        }
    }

    private GridCalendarView gridCalendarView;

    private void initView() {
        gridCalendarView = findViewById(R.id.gridMonthView);
        IDayTheme iDayTheme = new IDayTheme() {
            @Override
            public int colorSelectBG() {
                return 0xff2C9EF4;
            }

            @Override
            public int colorSelectDay() {
                return 0xffffffff;
            }

            @Override
            public int colorToday() {
                return 0xffff0000;
            }

            @Override
            public int colorMonthView() {
                return 0xffffffff;
            }

            @Override
            public int colorWeekday() {
                return 0xff2C3135;
            }

            @Override
            public int colorWeekend() {
                return 0xff2C3135;
            }

            @Override
            public int colorDecor() {
                return 0xffE81212;
            }

            @Override
            public int colorRest() {
                return 0;
            }

            @Override
            public int colorWork() {
                return 0;
            }

            @Override
            public int colorDesc() {
                return 0x00000000;
            }

            @Override
            public int sizeDay() {
                return DensityUtil.dip2px(CheckPlanActivity.this, 12);
            }

            @Override
            public int sizeDesc() {
                return 0;
            }

            @Override
            public int sizeDecor() {
                return DensityUtil.dip2px(CheckPlanActivity.this, 3);
            }

            @Override
            public int dateHeight() {
                return getDateHeight();
            }

            @Override
            public int colorLine() {
                return 0x00000000;
            }

            @Override
            public int smoothMode() {
                return 1;
            }
        };

        IWeekTheme iWeekTheme = new IWeekTheme() {
            @Override
            public int colorTopLinen() {
                return 0xffEBEDF0;
            }

            @Override
            public int colorBottomLine() {
                return 0x00000000;
            }

            @Override
            public int colorWeekday() {
                return 0xff7F8FA4;
            }

            @Override
            public int colorWeekend() {
                return 0xff7F8FA4;
            }

            @Override
            public int colorWeekView() {
                return 0xffffffff;
            }

            @Override
            public int sizeLine() {
                return DensityUtil.dip2px(CheckPlanActivity.this, 1);
            }

            @Override
            public int sizeText() {
                return DensityUtil.dip2px(CheckPlanActivity.this, 6);
            }
        };
        gridCalendarView.setDayTheme(iDayTheme);
        gridCalendarView.setWeekTheme(iWeekTheme);
        gridCalendarView.setDateClick(new MonthView.IDateClick() {

            @Override
            public void onClickOnDate(int year, int month, int day) {
                Intent intent = new Intent(CheckPlanActivity.this, CheckPlanDetailActivity.class);
                intent.putExtra("date", year + "-" + parseNumber(month) + "-" + parseNumber(day));
                startActivity(intent);
            }
        });

        gridCalendarView.setOnMonthSelectListener(new GridCalendarView.OnMonthSelectListener() {
            @Override
            public void onMonthSelect(String date) {
                getMonthPlan(gridCalendarView.getCurrentMonth());
            }
        });

        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        int currMonth = calendar.get(Calendar.MONTH) + 1;
        getMonthPlan(currYear + "-" + currMonth);
    }

    private int getDateHeight() {
        int width = LoadingActivity.screenWidth - 2 * DensityUtil.dip2px(this, 20);
        return width / 7;
    }

    public void back(View view) {
        finish();
    }

    private void getMonthPlan(String date) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("planTime", date);
        params.put("userId", SPHelper.getUid());

        helper.sendPOSTRequest(ApiDefine.TASK_MONTH, MonthPlanBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    MonthPlanBean bean = (MonthPlanBean) data;
                    if(bean.code.equals("0")){
                        initCal(bean);
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(CheckPlanActivity.this);
                    }

                }
            }
        });
    }

    private void initCal(MonthPlanBean bean) {
        List<CalendarInfo> list = new ArrayList<>();
        List<MonthPlanBean.PlanData> rows = bean.rows;
        if (rows != null && rows.size() > 0) {
            for (MonthPlanBean.PlanData planData : rows) {
                String[] dateArr = planData.planTime.split("-");
                list.add(new CalendarInfo(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]), "1"));
            }
        }
        gridCalendarView.setCalendarInfos(list);
    }
}
